<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true">
      <el-form-item v-if="!compName" label="客户名称" prop="compName">
        <el-input
          v-model="queryParams.compName"
          placeholder="请输入客户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务类型" prop="type">
        <el-select
          v-model="queryParams.type"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_business_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务状态" prop="status">
        <el-select
          v-model="queryParams.status"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_data_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:task:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="selected.length === 0"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      ref="table"
      v-loading="loading"
      :data="list"
      @selection-change="(selection) => selected = selection"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column v-if="!corpName" label="客户名称" prop="corpName" />
      <el-table-column label="任务名称" prop="taskName" />
      <el-table-column label="任务类型">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_business_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="当前状态" width="50">
          <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_task_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.begin_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.end_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:task:edit']"
            size="mini"
            type="text"
            :icon="scope.row.index === 0 ? 'el-icon-switch-button' : 'el-icon-video-play'"
            @click="handleOperate(scope.row)"
          >{{ scope.row.index === 0 ? '结束' : '启动' }}</el-button>
          <el-button
            v-hasPermi="['system:task:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['system:task:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="goDetailPage(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item v-if="!compName" label="客户名称" prop="compName">
          <el-input v-model="form.compName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="任务类型" prop="type">
          <el-select
            v-model="form.type"
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.sys_business_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="beginTime">
          <el-date-picker
            v-model="form.beginTime"
            type="datetime"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status" required>
          <el-select
            v-model="form.status"
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.sys_data_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { statusValidator } from '@/utils/validate'

export default {
  name: 'ServingList',
  dicts: ['sys_business_type', 'sys_task_status'],
  props: {
    compName: { // 不为空说明是在客户详情页，没有搜索等功能
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      selected: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        compName: this.compName || undefined,
        taskName: undefined,
        type: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskName: [
          { required: true, message: '任务名称不能为空', trigger: 'blur' }
        ],
        compName: [
          { required: true, message: '客户名称不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '任务类型不能为空', trigger: 'blur' }
        ],
        beginTime: [
          { type: 'date', required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        endTime: [
          { type: 'date', required: true, message: '结束时间不能为空', trigger: 'blur' }
        ],
        status: [
          { validator: statusValidator, message: '状态不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listRole(this.queryParams).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const roleId = row.roleId
      getRole(roleId).then(response => {
        this.form = {
          ...response.data,
          status: `${response.data.status}`
        }
        this.open = true
        this.title = '修改任务'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const formData = {
            id: '',
            ...this.form,
            status: parseInt(this.form.status)
          }
          if (this.form.id) {
            updateRole(formData).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addRole(formData).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.selected.map(v => v.id)
      this.$modal.confirm(`确认删除 所选 数据？`).then(function() {
        return delRole(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
        this.$refs.table.clearSelection()
        this.selected = []
      }).catch(() => {})
    },
    handleOperate(row) {
      this.$modal.confirm('是否确认启动任务？').then(function() {
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('启动成功')
      })
    },
    goDetailPage(row) {
      this.$router.push({
        name: 'ServingDetail',
        params: {
          taskNo: row.taskNo || '1',
          type: Math.random() > 0.5 ? 'env' : 'security'
        }
      })
    }
  }
}
</script>

